//
//  BaseTabBarViewController.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/10/31.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "LBNavigationController.h"
#import "PlayViewController.h"
#import "MusicHallController.h"
#import "MeViewController.h"
#import "CTTabBarItem.h"

#define kTabbarH 49
#define kBtnW (ScWidth/3)
#define KBtnH 49

@interface BaseTabBarViewController ()<UITabBarControllerDelegate,UINavigationControllerDelegate>{
   NSInteger selectIndex;
    BOOL     tabBarIsShow;
}
@property (nonatomic,strong) UIButton *button;
@end

@implementation BaseTabBarViewController

- (instancetype)init {
    if(self = [super init]) {
        selectIndex = 0;
        tabBarIsShow = YES;
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    selectIndex = 0;
    tabBarIsShow = YES;
    
    //添加viewController
    [self setTabBarVC];
    //隐藏系统uitabbar
    [self hideSystemTabbar];
    //创建自定义的tabbar
    [self createCustomTabbar];
    //添加中心
    [self setup];
    //设置通知
    [self addButtonNotifation];
}

//添加大圆按钮的通知
-(void)addButtonNotifation{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideTabBar) name:kTabbarCenterHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showTabBar) name:kTabbarCenterShowNotification object:nil];
}

// 初始化所有子控制器
- (void)setTabBarVC{
    
    [self setTabBarChildController:[[MusicHallController alloc] init] title:@"音乐馆" image:@"tab_feed" selectImage:nil];
    
    [self setTabBarChildController:[[UIViewController alloc] init] title:@"播放" image:nil selectImage:nil];
    
    [self setTabBarChildController:[[MeViewController alloc] init] title:@"我" image:@"tab_feed_profile" selectImage:nil];
}


// 添加tabbar的子viewcontroller
- (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr{
    
    LBNavigationController* nav = [[LBNavigationController alloc] initWithRootViewController:controller];
    nav.delegate = self;
    /**
    nav.tabBarItem.title = title;
    
    nav.tabBarItem.image = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    if (selectImageStr) {
        nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithRed:74/255.0 green:74/255.0 blue:74/255.0 alpha:1.0]} forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.0 green:200/255.0 blue:0 alpha:1.0]} forState:UIControlStateSelected];
     **/
    
    [self addChildViewController:nav];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark- setup
-(void)setup
{
    //  添加突出按钮
    [self addCenterButtonWithImage:[UIImage imageNamed:@"camera_button_take"] selectedImage:[UIImage imageNamed:@"tabBar_cameraButton_ready_matte"]];
}
#pragma mark - addCenterButton
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
    self.button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    
    //  设定button大小为适应图片
    self.button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [self.button setImage:buttonImage forState:UIControlStateNormal];
    [self.button setImage:selectedImage forState:UIControlStateSelected];
    
    
    //  这个比较恶心  去掉选中button时候的阴影
    self.button.adjustsImageWhenHighlighted=NO;
    /*
     *  核心代码：设置button的center 和 tabBar的 center 做对齐操作， 同时做出相对的上浮
     */
    CGPoint center = self.tabBarBg.center;
    center.y = center.y - buttonImage.size.height/2.8;
    self.button.center = center;
    [self.view addSubview:self.button];
    
    
}

-(void)pressChange:(id)sender
{
    [self selectedItemTabBarIndex:MusicTabIndex_Player];
}


//创建自定义的tabbar
- (void)createCustomTabbar {
    
    self.tabBarBg = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height-kTabbarH, ScWidth, kTabbarH)];
    self.tabBarBg.backgroundColor =UIColorMakeRGB(248, 248, 248);
    
    //画线
    UILabel *labLine=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScWidth, 0.5)];
    labLine.backgroundColor=UIColorMakeRGB(178, 178, 178);
    [self.tabBarBg addSubview:labLine];
    [self.view addSubview:self.tabBarBg];
    
    //音乐馆
    CTTabBarItem * homeBtn = [[CTTabBarItem alloc]initWithFrame:CGRectMake(0, 0 , kBtnW, KBtnH)];
    homeBtn.tag = 0;
    [homeBtn setNormalImage:[UIImage imageNamed:@"tab_feed"]
              selectedImage:[UIImage imageNamed:@"tab_feed"]
                      title:@"音乐馆"]; //tab_keypad_down  tab_keypad_up
    
    [homeBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarBg addSubview:homeBtn];
    homeBtn.selected = YES;
    
    //播放
    CTTabBarItem *contactBtn= [[CTTabBarItem alloc]initWithFrame:CGRectMake(1*kBtnW, 0, kBtnW, KBtnH)];
    contactBtn.tag = 1;
    [contactBtn setNormalImage:[UIImage imageNamed:@"tab_contact_nor"]
                 selectedImage:[UIImage imageNamed:@"tab_contact_sel"]
                         title:@"播放" ];
    [contactBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarBg addSubview:contactBtn];
    

    //我
    CTTabBarItem *meBtn=[[CTTabBarItem alloc]initWithFrame:CGRectMake(2*kBtnW, 0, kBtnW, KBtnH)];
    meBtn.tag=2;
    [meBtn setNormalImage:[UIImage imageNamed:@"tab_feed_profile"]
            selectedImage:[UIImage imageNamed:@"tab_feed_profile"]
                    title:@"我" ];
    [meBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBarBg addSubview:meBtn];
}




#pragma mark - 自定义tabbar按钮点击事件
- (void)tabBarBtnClick:(CTTabBarItem *)btn {
    if(btn.tag != selectIndex) {
        for(UIView *view in self.tabBarBg.subviews) {
            if([view isKindOfClass:[UIButton class]]) {
                CTTabBarItem *button = (CTTabBarItem *)view;
                button.selected = NO;
            }
        }

        if (btn.tag == MusicTabIndex_Player) {
            self.button.selected=YES;
            PlayViewController *controller=[[PlayViewController alloc] init];
            [self presentViewController:controller animated:YES completion:nil];
        }
        btn.selected = YES;
        selectIndex = btn.tag;
        self.selectedIndex=btn.tag;
    }
}


#pragma mark - 隐藏系统的tabbar
- (void)hideSystemTabbar {
    if ( [self.view.subviews count] < 2 ) {
        return;
    }
    
    UIView *contentView;
    if ( [[self.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ) {
        contentView = [self.view.subviews objectAtIndex:1];
    } else {
        contentView = [self.view.subviews objectAtIndex:0];
    }
    BOOL hide=YES;
    if (hide) {
        contentView.frame = self.view.bounds;
    } else {
        contentView.frame = CGRectMake(self.view.bounds.origin.x,
                                       self.view.bounds.origin.y,
                                       self.view.bounds.size.width,
                                       self.view.bounds.size.height -
                                       self.tabBar.frame.size.height);
    }
    
    for(UIView *view in self.view.subviews) {
        if([view isKindOfClass:[UITabBar class]]) {
            view.hidden = YES;
            break;
        }
    }
}

#pragma mark -公有方法

/**
 *  设置选中tabItem项
 *
 *  @param index 选中的第几个tab
 */
- (void)selectedItemTabBarIndex:(MusicTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (index==button.tag) {
                [self tabBarBtnClick:button];
                break;
            }
        }
    }
}

//显示微章
- (void)showBadgeValue:(NSString*)badgeValue index:(NSInteger)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button showBadgeValue:badgeValue];
                break;
            }
        }
    }
}

//移除微章
- (void)removeBadgeValueWithIndex:(MusicTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button removeBadgeValue];
                break;
            }
        }
    }
}

//显示红点
- (void)showBadgeWithIndex:(MusicTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button showBadge];
                break;
            }
        }
    }
}

//删除红点
- (void)removeBadgeWithIndex:(MusicTabIndex)index {
    for(UIView *view in self.tabBarBg.subviews) {
        if([view isKindOfClass:[UIButton class]]) {
            CTTabBarItem *button = (CTTabBarItem *)view;
            if (button.tag==index) {
                [button removeBadge];
                break;
            }
        }
    }
}

- (void)modifyTabBarFrame {
    CGRect r = self.tabBarBg.frame;
    r.origin.y = ScHeight-kTabbarH;
    self.tabBarBg.frame = r;
    self.view.frame = CGRectMake(0, 0, ScWidth, ScHeight-kTabbarH);
}

- (void)restoreTabBarFrame {
    CGRect r = self.tabBarBg.frame;
    r.origin.y = ScHeight-kTabbarH-64;
    self.tabBarBg.frame = r;
    
    self.view.frame = CGRectMake(0.0f, 0.0f, ScWidth, ScHeight-kTabbarH-64);
}

- (void)hideTabBar {
    if (!tabBarIsShow)
    {
        return;
    }
    [UIView animateWithDuration:0.35
                     animations:^{
                         CGRect tabFrame = self.tabBarBg.frame;
                         tabFrame.origin.x = 0 - tabFrame.size.width;
                         self.tabBarBg.frame = tabFrame;
                         self.button.alpha = 0.0;
                     }];
    tabBarIsShow = NO;
}

- (void)showTabBar {
    if (tabBarIsShow)
    {
        return;
    }
    [UIView animateWithDuration:0.35
                     animations:^{
                         
                         CGRect tabFrame = self.tabBarBg.frame;
                         tabFrame.origin.x = CGRectGetWidth(tabFrame) + CGRectGetMinX(tabFrame);
                         self.tabBarBg.frame = tabFrame;
                         self.button.alpha = 1.0;
                     }];
    tabBarIsShow = YES;
}

#pragma mark -UINavigationControllerDelegate Methods
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    if (navigationController.viewControllers.count >1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kTabbarCenterHideNotification object:nil];
    }

    if (navigationController.viewControllers.count == 1) {
         [[NSNotificationCenter defaultCenter] postNotificationName:kTabbarCenterShowNotification object:nil];
    }
    NSLog(@"navigationController.viewControllers.count =%d",navigationController.viewControllers.count);
    NSLog(@"willShowViewController class =%@",[viewController class]);
}

@end
